const trapList = require("./trapList.json");
const limit = 6;
ygopro.i18ns["en-us"].invalid_deck_rule =
  "More Trap cards are contained in your deck.";
ygopro.i18ns["zh-cn"].invalid_deck_rule =
  "卡组中的坑超出群娱乐表规定的数量，请检查卡组。";
ygopro.reloadI18nR();

ygopro.ctos_follow_after(
  "UPDATE_DECK",
  true,
  (buffer, info, client, server, datas) => {
    var room = ROOM_all[client.rid];
    if (!room) {
      return null;
    }
    if (room.duel_stage != ygopro.constants.DUEL_STAGE.BEGIN) {
      return null;
    }
    if (client.is_local) {
      return null;
    }
    if (
      room.name.toUpperCase().includes("LF2") ||
      room.name.toUpperCase().includes("NF") ||
      room.name.toUpperCase().includes("AI") ||
      room.name.toUpperCase().includes("DC") ||
      client.name.toUpperCase().startsWith("DC")
    ) {
      return null;
    }
    if (info.mainc > 256 || info.sidec > 256) {
      // Prevent attack, see https://github.com/Fluorohydride/ygopro/issues/2174
      return null;
    }
    let count = 0;
    for (var code of trapList) {
      count += client.main.filter((o) => {
        return o === code;
      }).length;

      if (count > limit) {
        ygopro.stoc_send_chat_to_room(
          room,
          "${invalid_deck_rule}",
          ygopro.constants.COLORS.RED
        );
        ygopro.stoc_send(client, "ERROR_MSG", {
          msg: 2,
          code: 0,
        });
        return true;
      }
    }
    return null;
  }
);
